/**
 * 
 */
package trungsi.gea.photos.filter;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;


/**
 * @author trungsi
 *
 */
public class LogFilter implements Filter {

	private static final Logger log = Logger.getLogger(LogFilter.class);
	
	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		log.info(buildMsg((HttpServletRequest)arg0));
		arg2.doFilter(arg0, arg1);
	}

	private String buildMsg(HttpServletRequest arg0) {
		StringBuilder builder = new StringBuilder();
		builder.append(new Date()).append(' ').append(arg0.getRemoteAddr()).append(' ').append(arg0.getRequestURI());
		
		return builder.toString();
		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
